/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wbd_au;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import wbd_au.objects_mappings.Cwiczenia;
import wbd_au.objects_mappings.ObiektCwiczebny;
import wbd_au.objects_mappings.Oddzial;
import wbd_au.objects_mappings.Zolnierz;

/**
 *
 * @author Gospo
 */
public class ClientJPanel extends javax.swing.JPanel {

    String pesel;

    /**
     * Creates new form ClientJPanel
     */
    public ClientJPanel(JFrame frame, String pesel) {
        this.pesel = pesel;
        initComponents();
        myInitComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        daneOsoboweJPanel = new javax.swing.JPanel();
        zolnierzeImieJTextField = new javax.swing.JTextField();
        zolnierzeEmailJTextField = new javax.swing.JTextField();
        zolnierzeNrMieszkaniaJTextField = new javax.swing.JTextField();
        zolnierzeNrDomuJTextField = new javax.swing.JTextField();
        zolnierzeKodPocztowyJTextField = new javax.swing.JTextField();
        zolnierzeUlicaJTextField = new javax.swing.JTextField();
        zolnierzeMiastoJTextField = new javax.swing.JTextField();
        zolnierzeNrTelefonuJTextField = new javax.swing.JTextField();
        zolnierzePeselJTextField = new javax.swing.JTextField();
        zolnierzeNazwiskoJTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        cwiczeniaJPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        oddzialJPanel = new javax.swing.JPanel();
        oddzialyNazwaOddzialuJTextField = new javax.swing.JTextField();
        jLabel24 = new javax.swing.JLabel();
        jLabel33 = new javax.swing.JLabel();
        oddzialySpecjalizacjaJTextField = new javax.swing.JTextField();

        zolnierzeImieJTextField.setEditable(false);

        zolnierzeEmailJTextField.setEditable(false);

        zolnierzeNrMieszkaniaJTextField.setEditable(false);

        zolnierzeNrDomuJTextField.setEditable(false);

        zolnierzeKodPocztowyJTextField.setEditable(false);

        zolnierzeUlicaJTextField.setEditable(false);
        zolnierzeUlicaJTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                zolnierzeUlicaJTextFieldActionPerformed(evt);
            }
        });

        zolnierzeMiastoJTextField.setEditable(false);

        zolnierzeNrTelefonuJTextField.setEditable(false);

        zolnierzePeselJTextField.setEditable(false);

        zolnierzeNazwiskoJTextField.setEditable(false);

        jLabel1.setText("Imię");

        jLabel2.setText("Nazwisko");

        jLabel3.setText("Pesel");

        jLabel4.setText("Nr telefonu");

        jLabel5.setText("Miasto");

        jLabel6.setText("Ulica");

        jLabel7.setText("Kod Pocztowy");

        jLabel8.setText("Nr domu");

        jLabel9.setText("Nr mieszkania");

        jLabel10.setText("e-mail");

        javax.swing.GroupLayout daneOsoboweJPanelLayout = new javax.swing.GroupLayout(daneOsoboweJPanel);
        daneOsoboweJPanel.setLayout(daneOsoboweJPanelLayout);
        daneOsoboweJPanelLayout.setHorizontalGroup(
            daneOsoboweJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(daneOsoboweJPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(daneOsoboweJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(daneOsoboweJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(zolnierzeNrMieszkaniaJTextField, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(zolnierzeNrDomuJTextField)
                    .addComponent(zolnierzeKodPocztowyJTextField)
                    .addComponent(zolnierzeUlicaJTextField)
                    .addComponent(zolnierzeMiastoJTextField)
                    .addComponent(zolnierzeNrTelefonuJTextField)
                    .addComponent(zolnierzePeselJTextField)
                    .addComponent(zolnierzeNazwiskoJTextField)
                    .addComponent(zolnierzeImieJTextField)
                    .addComponent(zolnierzeEmailJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(212, Short.MAX_VALUE))
        );
        daneOsoboweJPanelLayout.setVerticalGroup(
            daneOsoboweJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(daneOsoboweJPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(daneOsoboweJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(zolnierzeImieJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(daneOsoboweJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(zolnierzeNazwiskoJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(daneOsoboweJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(zolnierzePeselJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(daneOsoboweJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(zolnierzeNrTelefonuJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(daneOsoboweJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(zolnierzeMiastoJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(daneOsoboweJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(zolnierzeUlicaJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(daneOsoboweJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(zolnierzeKodPocztowyJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(daneOsoboweJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(zolnierzeNrDomuJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(daneOsoboweJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(zolnierzeNrMieszkaniaJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(daneOsoboweJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(zolnierzeEmailJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Dane osobowe", daneOsoboweJPanel);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "nazwa obiektu", "typ obiektu", "data od", "data do"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout cwiczeniaJPanelLayout = new javax.swing.GroupLayout(cwiczeniaJPanel);
        cwiczeniaJPanel.setLayout(cwiczeniaJPanelLayout);
        cwiczeniaJPanelLayout.setHorizontalGroup(
            cwiczeniaJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(cwiczeniaJPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 373, Short.MAX_VALUE)
                .addContainerGap())
        );
        cwiczeniaJPanelLayout.setVerticalGroup(
            cwiczeniaJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(cwiczeniaJPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Ćwiczenia", cwiczeniaJPanel);

        oddzialyNazwaOddzialuJTextField.setEditable(false);

        jLabel24.setText("Nazwa oddzialu");

        jLabel33.setText("Specjalizacja");

        oddzialySpecjalizacjaJTextField.setEditable(false);
        oddzialySpecjalizacjaJTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                oddzialySpecjalizacjaJTextFieldActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout oddzialJPanelLayout = new javax.swing.GroupLayout(oddzialJPanel);
        oddzialJPanel.setLayout(oddzialJPanelLayout);
        oddzialJPanelLayout.setHorizontalGroup(
            oddzialJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(oddzialJPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(oddzialJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel24, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel33, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(oddzialJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(oddzialyNazwaOddzialuJTextField)
                    .addComponent(oddzialySpecjalizacjaJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(225, Short.MAX_VALUE))
        );
        oddzialJPanelLayout.setVerticalGroup(
            oddzialJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(oddzialJPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(oddzialJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(oddzialyNazwaOddzialuJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel24, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(oddzialJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(oddzialySpecjalizacjaJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel33, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(213, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Oddział", oddzialJPanel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void zolnierzeUlicaJTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_zolnierzeUlicaJTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_zolnierzeUlicaJTextFieldActionPerformed

    private void oddzialySpecjalizacjaJTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_oddzialySpecjalizacjaJTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_oddzialySpecjalizacjaJTextFieldActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel cwiczeniaJPanel;
    private javax.swing.JPanel daneOsoboweJPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel33;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JPanel oddzialJPanel;
    private javax.swing.JTextField oddzialyNazwaOddzialuJTextField;
    private javax.swing.JTextField oddzialySpecjalizacjaJTextField;
    private javax.swing.JTextField zolnierzeEmailJTextField;
    private javax.swing.JTextField zolnierzeImieJTextField;
    private javax.swing.JTextField zolnierzeKodPocztowyJTextField;
    private javax.swing.JTextField zolnierzeMiastoJTextField;
    private javax.swing.JTextField zolnierzeNazwiskoJTextField;
    private javax.swing.JTextField zolnierzeNrDomuJTextField;
    private javax.swing.JTextField zolnierzeNrMieszkaniaJTextField;
    private javax.swing.JTextField zolnierzeNrTelefonuJTextField;
    private javax.swing.JTextField zolnierzePeselJTextField;
    private javax.swing.JTextField zolnierzeUlicaJTextField;
    // End of variables declaration//GEN-END:variables

    private void myInitComponents() {

        try {
            int idOddzial;

            String tableName = new Zolnierz().getTableName();
            String query = "select "
                    + new Zolnierz().getAttrSelectList()
                    + " from " + tableName + " where pesel=" + pesel;
            Statement stmt = DBManager.con.createStatement();
            ResultSet rs = stmt.executeQuery(query);
            rs.next();
            Zolnierz zolnierz = (Zolnierz) new Zolnierz().read(rs);
            zolnierzeImieJTextField.setText(zolnierz.getImie());
            zolnierzeNazwiskoJTextField.setText(zolnierz.getNazwisko());
            zolnierzePeselJTextField.setText(String.valueOf(zolnierz.getPesel()));
            zolnierzeNrTelefonuJTextField.setText(String.valueOf(zolnierz.getNrTelefonu()));
            zolnierzeMiastoJTextField.setText(zolnierz.getMiasto());
            zolnierzeUlicaJTextField.setText(zolnierz.getUlica());
            zolnierzeKodPocztowyJTextField.setText(String.valueOf(zolnierz.getKodPocztowy()));
            zolnierzeNrDomuJTextField.setText(String.valueOf(zolnierz.getNrDomu()));
            zolnierzeNrMieszkaniaJTextField.setText(String.valueOf(zolnierz.getNrMieszkania()));
            zolnierzeEmailJTextField.setText(zolnierz.getEmail());
            idOddzial = zolnierz.getIdOddzial();

            tableName = new Oddzial().getTableName();
            query = "select "
                    + new Oddzial().getAttrSelectList()
                    + " from " + tableName + " where ID_ODDZIAL=" + idOddzial;
            stmt = DBManager.con.createStatement();
            rs = stmt.executeQuery(query);
            rs.next();
            Oddzial oddzial = (Oddzial) new Oddzial().read(rs);
            oddzialyNazwaOddzialuJTextField.setText(oddzial.getNazwaOddzialu());
            oddzialySpecjalizacjaJTextField.setText(oddzial.getSpecjalizacja());
            DefaultTableModel model = (DefaultTableModel) jTable1.getModel();

            tableName = new Cwiczenia().getTableName();
            query = "select "
                    + new Cwiczenia().getAttrSelectList()
                    + " from " + tableName + " where ID_ODDZIAL=" + idOddzial;
            stmt = DBManager.con.createStatement();
            rs = stmt.executeQuery(query);


            while (rs.next()) {
                Cwiczenia cwiczenia = (Cwiczenia) new Cwiczenia().read(rs);
                tableName = new ObiektCwiczebny().getTableName();
                query = "select "
                        + new ObiektCwiczebny().getAttrSelectList()
                        + " from " + tableName + " where " + ObiektCwiczebny.Attribute.ID_OBIEKT + "=" + cwiczenia.getIdObiekt();
                stmt = DBManager.con.createStatement();
                ResultSet rs2 = stmt.executeQuery(query);
                rs2.next();
                ObiektCwiczebny obiektCwiczebny = (ObiektCwiczebny) new ObiektCwiczebny().read(rs2);

                Object[] values = new Object[]{obiektCwiczebny.getNazwa(), obiektCwiczebny.getTyp(), cwiczenia.getDataOd(), cwiczenia.getDataDo()};
                model.addRow(values);
            }
        } catch (Exception ex) {
            Logger.getLogger(ClientJPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
